package com.css.fxfzdpzs.modules.fwss.repository;

import com.css.fxfzdpzs.modules.fwss.entity.first.*;
import com.css.fxfzdpzs.modules.fwss.entity.second.CollectionSituation;
import com.css.fxfzdpzs.modules.fwss.entity.second.FwSiftCoverage;
import com.css.fxfzdpzs.modules.fwss.entity.second.GeneralSurveyStatistics;
import com.css.fxfzdpzs.modules.fwss.entity.second.HousingStatistics;
import com.css.fxfzdpzs.modules.fwss.vo.FwProvinceSamplingRateVO;
import com.css.fxfzdpzs.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @description
 */
@Repository
public class FwssRepositoryImpl implements FwssRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public List<FwCollectionSystemSituation> getSystemSituation() {
        String sql = "select * from fw_collection_system_situation ";
        List<FwCollectionSystemSituation> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwCollectionSystemSituation.class));
        return list;
    }

    @Override
    public List<Fw5yearsNum> getYearsNum() {
        String sql = "select * from fw_5years_num  order by  collect_year limit 5 offset 0 ";
        List<Fw5yearsNum> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Fw5yearsNum.class));
        return list;
    }

    @Override
    public List<FwProvinceSamplingRate> getNumCoverage() {
        String sql = "select * from fw_province_num_coverage  ";
        List<FwProvinceSamplingRate> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwProvinceSamplingRate.class));
        return list;
    }

    @Override
    public FwProvinceSamplingRateVO getCountrySituation() {
        String sql = "select * from fw_country_situation  ";
        List<FwProvinceSamplingRateVO> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwProvinceSamplingRateVO.class));
        if (PlatformObjectUtils.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<FwProvinceDongAndArea> getSubmitSituation(String xOrp, String aOrc) {
        StringBuilder sql = new StringBuilder(" select ");
        //xOrp详查0或普查1    aOrc全部0或审核1
        if (aOrc.equals("0")) {
            //全部
            sql.append("  province,all_dong as dong, all_area as area from fw_province_submit_situation  ");
        }
        if (aOrc.equals("1")) {
            //审核
            sql.append("province,check_dong as dong,check_area as area from fw_province_submit_situation ");
        }
        sql.append(" where flag='" + xOrp + "'");
        List<FwProvinceDongAndArea> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FwProvinceDongAndArea.class));
        return list;
    }

    @Override
    public List<GeneralSituation> getGeneralLibrarySituation(String s) {
        StringBuilder sql = new StringBuilder(" select ");
        if (s.equals("0")) {
            //全部
            sql.append(" all_dong as dong,all_area as area from fw_general_library_situation ");
        }
        if (s.equals("1")) {
            sql.append(" check_dong as dong,check_area as area from fw_general_library_situation ");
        }
        List<GeneralSituation> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(GeneralSituation.class));
        return list;
    }

    @Override
    public List<SiftSituation> getSiftLibrarySituation(String s) {
        StringBuilder sql = new StringBuilder(" select ");
        if (s.equals("0")) {
            //全部
            sql.append(" all_dong as dong,all_area as area,all_dong_rate as dong_rate,all_area_rate as area_rate  from fw_sift_library_situation ");
        }
        if (s.equals("1")) {
            sql.append(" check_dong as dong,check_area as area, check_dong_rate as dong_rate,check_area_rate as area_rate   from fw_sift_library_situation ");
        }
        List<SiftSituation> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(SiftSituation.class));
        return list;
    }

    @Override
    public List<HousingStatistics> getStatistics() {
        String sql = "select * from fw_housing_statistics";
        List<HousingStatistics> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(HousingStatistics.class));
        return list;
    }

    @Override
    public List<CollectionSituation> getSituation() {
        String sql = "select * from fw_collection_situation";
        List<CollectionSituation> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CollectionSituation.class));
        return list;
    }

    @Override
    public List<GeneralSurveyStatistics> getData1(String classify, List<String> list) {
        String sql = "select * from fw_general_survey_statistics where classify = :classify and category in (:list)";
        Map<String, Object> map = new HashMap<>();
        map.put("classify",classify);
        map.put("list",list);
        List<GeneralSurveyStatistics> list1 = namedParameterJdbcTemplate.query(sql,map, new BeanPropertyRowMapper<>(GeneralSurveyStatistics.class));
        return list1;
    }

    @Override
    public List<FwMapDate> getMapDate(String flag) {
        String sql = "select * from fw_map_date where flag ='"+flag+"'";
        List<FwMapDate> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FwMapDate.class));
        return list;
    }

    @Override
    public List<FwSiftCoverage> getSiftCoverage(String s) {
        StringBuilder sql = new StringBuilder(" select ");
        if (s.equals("0")) {
            //面积全部
            sql.append(" id , province, all_area as coverage  from fw_province_sift_coverage ");
        }
        if (s.equals("1")) {
            //面积已审核
            sql.append(" id , province, check_area as coverage  from fw_province_sift_coverage ");
        }
        if (s.equals("2")) {
            //面积全部
            sql.append(" id , province, all_dong as coverage  from fw_province_sift_coverage ");
        }
        if (s.equals("3")) {
            //面积已审核
            sql.append(" id , province, check_dong as coverage  from fw_province_sift_coverage ");
        }
        List<FwSiftCoverage> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(FwSiftCoverage.class));
        return list;
    }
}
